package com.ruige.service.service;

import com.ruige.service.config.RuigeClientConfig;
import com.ruige.service.config.RuigeServerConfig;
import com.ruige.service.listenner.WebConfigListener;
import com.ruige.service.pojo.Result;
import com.ruige.service.template.RuigeTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 服务类
 */
@Service
public class RuigeService {
    @Autowired
    private RuigeServerConfig serverConf;
    @Autowired
    private RuigeClientConfig clientConf;
    @Autowired
    private WebConfigListener webConfigListener;
    @Autowired
    private RuigeTemplate ruigeTemplate;


    /**
     * 封装服务端服务信息
     *
     * @return
     */
    public com.ruige.service.pojo.Service getServerService() {
        serverConf.setIp(webConfigListener.getIp());
        serverConf.setPort(webConfigListener.getPort());
        return new com.ruige.service.pojo.Service(serverConf.ip, serverConf.port, serverConf.service_name, serverConf.server_list.toString(), serverConf.type);
    }

    /**
     * 封装客户端服务信息
     *
     * @return
     */
    public com.ruige.service.pojo.Service getClientService() {
        return new com.ruige.service.pojo.Service(clientConf.ip, clientConf.port, clientConf.client_name, clientConf.getRequire_server(), clientConf.type);
    }

    /**
     * 注册服务
     */
    public void register() {

        com.ruige.service.pojo.Service serverService = this.getServerService();
        ruigeTemplate.register(serverConf.getGoreka_address(), "add", serverService);

        com.ruige.service.pojo.Service clientService = this.getClientService();
        ruigeTemplate.register(serverConf.getGoreka_address(), "add", clientService);

    }

    /**
     * 心跳服务
     */
    public void ping() {
        com.ruige.service.pojo.Service serverService = this.getServerService();
        ruigeTemplate.ping(serverConf.getGoreka_address(), "ping", serverService);

        com.ruige.service.pojo.Service clientServcie = this.getClientService();
        ruigeTemplate.ping(serverConf.getGoreka_address(), "ping", clientServcie);
    }


    public Result findAll() {
        return ruigeTemplate.findAll(serverConf.getGoreka_address(), "findAll");
    }

}
